package org.jwebuilder.asm;

import java.io.Serializable;
import java.util.ArrayList;

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Type;

public class SerializableClassAdapter extends ClassAdapter
{
	private static final String internalNameForSerializable = Type.getInternalName(Serializable.class);

	public SerializableClassAdapter(ClassVisitor cv)
	{
		super(cv);
	}

	@Override
	public void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
	{
		ArrayList<String> interfaceList = new ArrayList<String>(interfaces == null ? 1 : interfaces.length);
		boolean exist = false;
		if(interfaces != null)
		{
			for(int i = 0; i < interfaces.length; i++)
			{
				interfaceList.add(interfaces[i]);
				if(internalNameForSerializable.equals(interfaces[i]))
				{
					exist = true;
				}
			}
		}
		if(!exist)
		{
			interfaceList.add(internalNameForSerializable);
		}
		String[] extInterfaces = new String[interfaceList.size()];
		super.visit(version, access, name, signature, superName, interfaceList.toArray(extInterfaces));
	}
}